<template>
  <el-dialog :title="title" :visible="isShow" @close="closeEvent">
    <el-form ref="addform" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="closeEvent">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysrolePost, sysroleEdit } from '@/api/setting'
export default {
  name: '',
  // model: {
  //   prop: 'isShow',
  //   event: 'input'
  // },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      mode: 'add',
      // lookShow: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'change' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    title() {
      return {
        add: '新增部门',
        edit: '编辑部门'
      }[this.mode]
    }
  },
  created() {},
  methods: {
    closeEvent() {
      // this.$emit('input', false)
      this.$emit('close')
      this.$refs.addform.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // 确定按钮
    submit() {
      this.$refs.addform.validate(async(valid) => {
        if (valid) {
          if (this.mode === 'add') {
            // 调用新增角色api
            await sysrolePost(this.form)
            this.$message.success('新增成功')
          } else if (this.mode === 'edit') {
            await sysroleEdit(this.form)
            this.$message.success('编辑成功')
          }

          // 关闭弹框
          this.closeEvent()
          this.$emit('getData')
          // this.$parent('getData')
        }
      })
    }
  }
}
</script>
<style lang="less" scoped></style>
